package com.one.blocks.rbac.controller;

import com.one.blocks.mvc.controller.AbstractController;
import com.one.blocks.mvc.request.PageRequest;
import com.one.blocks.mvc.response.ResponseHelper;
import com.one.blocks.mvc.response.WebPageResponse;
import com.one.blocks.mvc.response.WebResponse;
import com.one.blocks.rbac.controller.convert.SysRoleConvert;
import com.one.blocks.rbac.controller.query.SysRoleQuery;
import com.one.blocks.rbac.controller.vo.SysRoleVo;
import com.one.blocks.rbac.domain.SysRole;
import com.one.blocks.rbac.manager.SysRoleManager;
import com.one.blocks.rbac.manager.SysUserRoleManager;
import com.one.blocks.security.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * @author <a href="mailto:idler41@163.con">idler41</a> created on 2022-05-01 10:06:14
 */
@Api(tags = "系统角色接口")
@RestController
@RequestMapping("/system/role")
@Slf4j
@Validated
public class SysRoleController extends AbstractController<SysRoleManager, SysRoleConvert, SysRole, SysRoleVo, SysRoleQuery> {

    @Autowired
    private SysUserRoleManager sysUserRoleManager;

    @Autowired
    public SysRoleController(SysRoleManager manager) {
        super(manager, SysRoleConvert.INSTANCE);
    }

    @ApiOperation("分页查询角色")
    @PreAuthorize("system:role:view")
    @GetMapping("/page")
    public WebPageResponse<SysRoleVo> page(@Validated PageRequest request, @Validated SysRoleQuery query) {
        return super.generalPage(request, query);
    }

    @ApiOperation("查询单个角色")
    @PreAuthorize("system:role:view")
    @GetMapping
    public WebResponse<SysRoleVo> get(@Validated @NotNull @RequestParam Integer id) {
        return super.generalGet(id);
    }

    @ApiOperation("根据id批量查询角色")
    @PreAuthorize("system:role:view")
    @GetMapping("/batch")
    public WebResponse<List<SysRoleVo>> getIds(@Validated @NotNull @Size(min = 1, max = 100) @RequestParam("ids[]") Set<Integer> ids) {
        return super.generalGetIds(ids);
    }

    @ApiOperation("添加角色")
    @PreAuthorize("system:role:insert")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Serializable> insert(@Validated @RequestBody SysRoleVo sysRoleVo) {
        return super.generalInsert(sysRoleVo);
    }

    @ApiOperation("更新角色(更新所有字段)")
    @PreAuthorize("system:role:update")
    @PutMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Boolean> update(@Validated @RequestBody SysRoleVo sysRoleVo) {
        return super.generalUpdate(sysRoleVo);
    }

    @ApiOperation("更新角色(只更新非空字段)")
    @PreAuthorize("system:role:update")
    @PatchMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Boolean> updateSelective(@Validated @RequestBody SysRoleVo sysRoleVo) {
        return super.generalUpdateSelective(sysRoleVo);
    }

    @ApiOperation("删除角色")
    @PreAuthorize("system:role:delete")
    @DeleteMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Integer> delete(@Validated @NotNull @Size(min = 1, max = 100) @RequestBody Set<Integer> ids) {
        return super.generalDeleteByIds(ids);
    }

    @ApiOperation("根据指定用户id查询角色")
    @PreAuthorize("system:role:view")
    @GetMapping(value = "/user")
    public WebResponse<SysRoleVo> getByUserId(@Validated @NotNull @RequestParam Integer userId) {
        return ResponseHelper.success(convert.entityToVo(manager.getByUserId(userId)));
    }
}